﻿using System;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using InitApp.IView;
using System.Windows.Forms;
using WeatherModel;
using WeatherUSB;

namespace InitApp.Controller
{
    public class DataBaseController
    {
        private IMainFormView m_View;

        public DataBaseController(IMainFormView view)
        {
            m_View = view;
        }

        public void InitDataBase()
        {
            var source = new InPlaceConfigurationSource();
            source.Add(typeof(ActiveRecordBase), AppGlobal.DataBaseConnection);
            ActiveRecordStarter.Initialize(
                new[]
                    {
                        typeof (ProSetup).Assembly,
                    },
                source);

            ActiveRecordStarter.CreateSchema();
            InitWeatherData();
            MessageBox.Show("初始化完成！");
        }


        private void InitWeatherData()
        {
            var nDate = DateTime.Now;
            var iMaxChannel = 3;
//            var iMaxChannel = 0;

            using (var tran = new TransactionScope())
            {
                try
                {
                    for (int iChannel = 0; iChannel <= iMaxChannel; iChannel++)
                    {
                        var nDay = nDate;
                        var iMaxYear = 365*4;
//                        var iMaxYear = 30;
                        for (int iDay = 0; iDay <= iMaxYear; iDay++)
                        {
                            var wt = new WeatherTime(iChannel);

                            wt.TemperatureMax = int.MinValue;
                            wt.TemperatureMin = int.MaxValue;

                            wt.HumidityMax = int.MinValue;
                            wt.HumidityMin = int.MaxValue;

                            wt.WindMax = int.MinValue;
                            wt.WindMin = int.MaxValue;

                            wt.RainMax = int.MinValue;
                            wt.RainMin = int.MaxValue;

                            wt.BarometerMax = int.MinValue;
                            wt.BarometerMin = int.MaxValue;

                            wt.RecodeDate = nDay;
                            wt.Create();
                            nDay = nDay.AddDays(1);
                        }
                    }
                    tran.VoteCommit();
                }
                catch (Exception ex)
                {
                    tran.VoteRollBack();
                    throw ex;
                }
            }

            using (var tran = new TransactionScope())
            {
                try
                {
                    for (int iChannel = 0; iChannel <= iMaxChannel; iChannel++)
                    {
                        var nDay = nDate;
                        var iMaxYear = 365*4;
//                        var iMaxYear = 30;
                        for (int iDay = 0; iDay <= iMaxYear; iDay++)
                        {
                            var nHour = nDay;

                            for (int iHour = 0; iHour < 24; iHour++)
                            {
                                InitWeatherDataItem(nHour, iChannel);
                                nHour = nHour.AddHours(1);
                            }

                            nDay = nDay.AddDays(-1);
                        }
                    }
                    tran.VoteCommit();
                }
                catch (Exception ex)
                {
                    tran.VoteRollBack();
                    throw ex;
                }
            }
        }

        private Random ran = new Random();
        private void InitWeatherDataItem(DateTime nHour, int iChannel)
        {
            var wd = new WeatherData();

            wd.Temperature = ran.Next(0, 100);
            wd.Humidity = ran.Next(100, 200);
            wd.Rain = ran.Next(200, 300);
            wd.Wind = ran.Next(300, 400);
            wd.Barometer = ran.Next(400, 500);

            wd.RecodeDate = nHour;
            wd.Channel = iChannel;

            int iTimes = ran.Next(2, 20);
            DateTime nHour1 = nHour.AddMinutes(1);

            for (int index = 0; index < iTimes;index ++ )
            {
                var wd1 = new WeatherData();

                wd1.Temperature = ran.Next(0, 100);
                wd1.Humidity = ran.Next(100, 200);
                wd1.Rain = ran.Next(200, 300);
                wd1.Wind = ran.Next(300, 400);
                wd1.Barometer = ran.Next(400, 500);

                wd1.RecodeDate = nHour1;
                nHour1 = nHour1.AddMinutes(1);
                wd1.Channel = iChannel;
                wd.AddData(wd1);
            }

                //            wd.CreateAndFlush();
                wd.Create();
        }

        public void OpenUSB()
        {
            MessageBox.Show(LoadData.OpenUSB());
        }

        public void CloseUSB()
        {
            MessageBox.Show(LoadData.CloseUSB());
        }

        public void DownUSB()
        {
            //MessageBox.Show(LoadData.DownUSB());
        }
    }
}